package com.spider.page;

import com.spider.pool.UriQueueScheduler;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.util.EntityUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.seimicrawler.xpath.JXDocument;
import org.seimicrawler.xpath.JXNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author β世界
 * Created on 17:04
 * @Description 每个URL对应的结果页面xx
 */
public class HtmlPage extends ResultPage {
    private static final Logger logger = LoggerFactory.getLogger(HtmlPage.class);
    /** 响应体 **/
    private String content;
    /** 网页解析后得到的节点 **/
    private Document document;
    /** 域名 **/
    private String domain;
    /** Jsoup的解析 **/
    private JXDocument jxDocument;

    /**
     * @author β世界 on 14:08 2020/2/8x
     * @Description 通过实体对象来构建不同的请求格式
     * @param response: 响应结果
     * @param httpRequestBase: 请求内容
     * @param uriQueueScheduler: 连接池
     **/
    public HtmlPage(CloseableHttpResponse response, HttpRequestBase httpRequestBase, UriQueueScheduler uriQueueScheduler) {
        super(response,httpRequestBase,uriQueueScheduler);
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            try {
                url = httpRequestBase.getURI().toURL();
                this.content = EntityUtils.toString(entity,"UTF-8");
                this.document = Jsoup.parse(this.content);
                this.jxDocument = JXDocument.create(document);
                setDomain(httpRequestBase.getURI());
            } catch (Exception e) {
                e.printStackTrace();
                uriQueueScheduler.addUrl(httpRequestBase.getURI().toString());
            }
        }
    }

    @Override
    public Object getContent() {
        return content;
    }

    /**
     * @author β世界 on 0:08 2020/2/12
     * @Description 设置域名
     * @param uri:uri链接对象
     * @return void
     **/
    private void setDomain(URI uri) {
        try {
            if (uri != null) {
                this.domain = uri.toURL().getProtocol() + "://" + uri.getHost();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @author β世界 on 13:34 2020/2/22
     * @Description 使用正则表达式来匹配内容
     * @param regex:正则表达式
     * @return java.lang.String[] 匹配到的所有内容
     **/
    @SuppressWarnings("unused ")
    public String[] regex(String regex) {
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(this.content);
        ArrayList<String> arrayList = new ArrayList<>();
        while (m.find()){
            arrayList.add(m.group());
        }

        return arrayList.toArray(new String[0]);
    }

    /**
     * @author β世界 on 23:05 2020/2/12
     * @Description 通过xpath语句来获取相应的内容，返回的是一个Element对象
     * @param xpath:Xpath语句
     * @return java.lang.String
     **/
    public List<Element> xpath(String xpath){
        List<JXNode> jxNodeList =  jxDocument.selN(xpath);
        List<Element> elementList = new ArrayList<>(jxNodeList.size());
        if(jxNodeList.size() != 0){
            for (JXNode jxNode : jxNodeList) {
                elementList.add(jxNode.asElement());
            }
        }

        return elementList;
    }

    /**
     * @author β世界 on 0:12 2020/2/12
     * @Description 获取所有的链接
     * @return java.util.ArrayList<java.lang.String>
     **/
    @SuppressWarnings("unused ")
    public ArrayList<String> getLink() {
        Elements els = this.document.select("a[href]");
        ArrayList<String> links = new ArrayList<>();
        for (Element el : els) {
            String link = el.attr("href");
            link = insertDomain(link);
            links.add(link);
        }
        return links;
    }

    /**
     * @author β世界 on 0:45 2020/2/13
     * @Description 为一个单独的链接加上域名
     * @param link:一串链接
     * @return java.lang.String
     **/
    public String insertDomain(String link){
        if (link.indexOf("/") != 0) {
            StringBuilder temp = new StringBuilder(link);
            temp.insert(0, "/");
            link = temp.toString();
        }
        return this.domain + link;
    }

}
